<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page session="true" %>
    <%@ page import="com.gri.entities.Project,com.gri.utils.GriUtils,java.sql.Connection,
    java.sql.PreparedStatement,java.sql.ResultSet,com.gri.entities.ProcessedData,java.util.ArrayList,java.util.List" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Project Detail</title>
<style type="text/css">
	table th{
	padding:5px;
	}
</style>
<link href="css/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
	function submitBackForm(){
		document.backForm.submit();
	}
</script>
</head>
<body>
<%
	String projectId = request.getParameter("proId");
	String noProjectMessage = "";
	List<ProcessedData> dataList = new ArrayList<ProcessedData>();
	String projectName = "";
	StringBuffer keywords = new StringBuffer();
	String projectUrl = "";
	if(projectId != null && !projectId.isEmpty()){
		Connection con = null;
		try{
			con = GriUtils.getConnection();
			PreparedStatement stmt = con.prepareStatement("Select * from tbl_processed_data where project_id=?");
			stmt.setInt(1, Integer.parseInt(projectId));
			ResultSet rs = stmt.executeQuery();
			ProcessedData processData = null;
			while(rs.next()){
				processData = new ProcessedData();
				processData.setAccessTime(rs.getDate("accessTime"));
				processData.setRank(rs.getString("rank"));
				processData.setCustId(rs.getInt("cust_id"));
				processData.setProjectId(rs.getInt("project_id"));
				processData.setProjectKeywords(rs.getString("keywords"));
				processData.setProjectName(rs.getString("project_name"));
				processData.setProjectUrl(rs.getString("url"));
				dataList.add(processData);
			}
		
			con.close();
			
			if(!dataList.isEmpty()){
				ProcessedData data = dataList.get(0);
				projectName = data.getProjectName();
				projectUrl = data.getProjectUrl();
				for(ProcessedData eachData : dataList){
					keywords.append(eachData.getProjectKeywords() + ",");
				}
			}
			
		}catch(Exception ex){
			System.out.println("Exception occured while retrieving project Details " + ex);
		}finally{
			try{
				if(con != null){
					con.close();
				}
			}catch(Exception ex){
				System.out.println("Exception occured while closing connection " + ex);
			}
		}
		
	} else {
		noProjectMessage= "Sorry, Project does not exists";
	}
	
	
	
%>
<div id="centered">
	<jsp:include page="facelets/image.jsp"></jsp:include>
	
	<jsp:include page="facelets/logout.jsp"></jsp:include>
	
	<div>
		<form name="backForm" method="post" action="backFromCreate.do"> 
			<input type="hidden" name="backLink" value="True" />
			<a href="javascript: submitBackForm()" >Back</a>
		</form>		
	</div>
	
	<div style="font-family:verdana; color:red;font-size: 12px; margin-top: 10px;">
		<%= noProjectMessage %>
	</div>
	
	<div style="height:300px;font-family: verdana; font-size:13px; width:75%;">
		<div style="text-align: left;padding-top: 10px;">
		<% if(!dataList.isEmpty()){ %>
			<table>
				<tr>
					<td>
						Project Name :
					</td>
					<td>
						<%= projectName %>
					</td>
				</tr>
				
				<tr>
					<td>
						Project Url :
					</td>
					<td>
						<%= projectUrl %>
					</td>
				</tr>
				
				<tr>
					<td>
						Project Keywords :
					</td>
					<td>
						<%= keywords.toString().substring(0, keywords.toString().length() -1) %>
					</td>
				</tr>
			</table>
			<% } %>
			</br>
			</br>
			
			<% if(!dataList.isEmpty()){
				
			%>
			<b>YOUR PROJECT TRACKING DETAILS ARE:</b>
			<table width="500px" border="1">
			<thead>
				<tr>
					<th>Project URL</th>
					<th>Keyword</th>
					<th>Accessed Date</th>
					<th>Position</th>
					
				</tr>
			</thead>
			<%
			for(ProcessedData proData : dataList){
			%>
			<tbody>
				<tr>
					<td>
						<%= proData.getProjectUrl() %>
					</td>
					<td>
						<%= proData.getProjectKeywords() %>
					</td>
					<td>
						<%= proData.getAccessTime() %>
					</td>					
					<td>
						<%= proData.getRank() %>
					</td>					
				</tr>
			</tbody>
			<%
			}
			%>
				
			</table>
			
			<%} else {
				
			%>
				
				<h4>YOU DONT HAVE ANY PROCESSED RECORDS</h4>	
				
			<%
			}
			%>
			
		</div>
	</div>
	<jsp:include page="facelets/footer.jsp"></jsp:include>
	</div>
</body>
</html>